package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

/**
 * 对象功能:  奖惩积分审批查询
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/19 09:43
 */
public class ScoreApprovalQueryFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      ScoreApprovalQueryFragment.class.getSimpleName() + "EXTRA_CONTENT";
  @BindView(R.id.tv_startDate) TextView tvStartDate;
  @BindView(R.id.ib_startDate) ImageView ibStartDate;
  @BindView(R.id.tv_endDate) TextView tvEndDate;
  @BindView(R.id.ib_endDate) ImageView ibEndDate;
  @BindView(R.id.tv_dataStatus) TextView tvDataStatus;
  @BindView(R.id.ib_dataStatus) ImageView ibDataStatus;
  @BindView(R.id.tv_scoreStaff) TextView tvScoreStaff;
  @BindView(R.id.ib_scoreStaff) ImageView ibScoreStaff;
  @BindView(R.id.tv_scoredStaff) TextView tvScoredStaff;
  @BindView(R.id.ib_scoredStaff) ImageView ibScoredStaff;
  private int REQUEST_STARTDATE = 0;
  private int REQUEST_ENDDATE = 1;
  private int REQUEST_SCORE = 2;
  private int REQUEST_SCORED = 3;
  private int REQUEST_STATUS = 4;

  private String scoredStaffCode;
  private String scoredStaffName;
  private String scoreStaffCode;
  private String scoreStaffName;
  private String startDate;
  private String endDate;
  private String dataStatus;
  private String dataStatusName;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    ExtraValueContent queryContent =
        (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    startDate = queryContent.getExtName1();
    endDate = queryContent.getExtName2();
    dataStatus = queryContent.getExtCode3();
    dataStatusName = queryContent.getExtName3();
    scoreStaffCode = queryContent.getExtCode4();
    scoreStaffName = queryContent.getExtName4();
    scoredStaffCode = queryContent.getExtCode5();
    scoredStaffName = queryContent.getExtName5();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menu_item_confirm) {
      volleyPostSure();
      return true;
    } else {
      return super.onOptionsItemSelected(item);
    }
  }

  private void volleyPostSure() {
    ExtraValueContent queryContent = new ExtraValueContent();
    queryContent.setExtName1(startDate);
    queryContent.setExtName2(endDate);
    queryContent.setExtName3(dataStatusName);
    queryContent.setExtCode3(dataStatus);
    queryContent.setExtName4(scoreStaffName);
    queryContent.setExtCode4(scoreStaffCode);
    queryContent.setExtName5(scoredStaffName);
    queryContent.setExtCode5(scoredStaffCode);
    Intent intent = new Intent();
    intent.putExtra(EXTRA_CONTENT, queryContent);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override protected int initLayoutId() {
    return R.layout.fragment_score_approval_query;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    tvStartDate.setText(startDate);
    tvEndDate.setText(endDate);
    tvDataStatus.setText(dataStatusName);
    tvScoreStaff.setText(scoreStaffName);
    tvScoredStaff.setText(scoredStaffName);
  }

  @OnClick({
      R.id.ib_startDate, R.id.ib_endDate, R.id.ib_dataStatus, R.id.ib_scoreStaff,
      R.id.ib_scoredStaff
  }) public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_startDate:
        showDialog(new BaseYMDTimeDialogFragment(), REQUEST_STARTDATE);
        break;
      case R.id.ib_endDate:
        showDialog(new BaseYMDTimeDialogFragment(), REQUEST_ENDDATE);
        break;
      case R.id.ib_dataStatus:
        showDialog(new ScoreApprovalStatusDialogFragment(), REQUEST_STATUS);
        break;
      case R.id.ib_scoreStaff:
        getPersonList(REQUEST_SCORE, scoreStaffCode);
        break;
      case R.id.ib_scoredStaff:
        getPersonList(REQUEST_SCORED, scoredStaffCode);
        break;
    }
  }

  private void getPersonList(int requestCode, String jobnos) {

    Intent person = new Intent(getActivity(), SearchPersonMultipleActicity.class);
    person.putExtra(SearchPersonMultipleFragment.EXTRA_FROM, SearchPersonListFragment.VALUE_COMMON);
    person.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, jobnos);
    startActivityForResult(person, requestCode);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_STARTDATE || requestCode == REQUEST_ENDDATE) {
      //开始和结束时间
      Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String dateStr = MyDateUtil.dateToString(date, MyDateUtil.TYPE_DATE);
      if (requestCode == REQUEST_STARTDATE) {
        startDate = dateStr;
        tvStartDate.setText(dateStr);
      }
      if (requestCode == REQUEST_ENDDATE) {
        endDate = dateStr;
        tvEndDate.setText(dateStr);
      }
    }

    if (requestCode == REQUEST_SCORE || requestCode == REQUEST_SCORED) {
      //奖惩人
      String mNames = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      String mJobNos = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);

      if (requestCode == REQUEST_SCORE) {
        scoreStaffCode = mJobNos;
        scoreStaffName = mNames;
        tvScoreStaff.setText(mNames);//奖惩人
      }
      if (requestCode == REQUEST_SCORED) {
        scoredStaffCode = mJobNos;
        scoredStaffName = mNames;
        tvScoredStaff.setText(mNames);//被奖惩人
      }
    }
    if (requestCode == REQUEST_STATUS) {
      CustomerDialog status = (CustomerDialog) data.getSerializableExtra(
          ScoreApprovalStatusDialogFragment.EXTRA_DATA_STATUS);
      dataStatus = status.getId();
      dataStatusName = status.getName();
      tvDataStatus.setText(status.getName());
    }
  }

  public static ScoreApprovalQueryFragment newInstance(ExtraValueContent queryContent) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryContent);
    ScoreApprovalQueryFragment fragment = new ScoreApprovalQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
